﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using FluentData;
using Framework.Data;

namespace Framework.Core
{
    internal class UserAuthDataAccess:BaseDataAccess
    {
        internal List<UserAuth> GetUserAuths(string userName)
        {
            var sql = "SELECT [iAuthID], [sContent], [dEndTime] FROM [User_Auth]"
                               + " WHERE [sAuthName]=@Name AND [dBeginTime]<=GETDATE() AND [dEndTime]>=GETDATE()";

            using (var context = DataHelper.GetBaseDbContext())
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Credential));
                var userAuths =
                    context.Sql(sql).Parameter("Name", userName).QueryMany<UserAuth>(delegate(UserAuth map, IDataReader reader)
                    {
                        map.AuthID = reader.GetInt64("iAuthID");
                        map.EndTime = reader.GetDateTime("dEndTime");
                        map.Credential = serializer.Deserialize(new StringReader(reader.GetString("sContent"))) as Credential;
                        //map.Content = reader.GetString("sContent");
                        //TODO:更多字段
                    });
                return userAuths;
            }
        }
    }
}
